package ct.producer.Bean;

import ct.common.bean.DataIn;
import ct.common.bean.DataOut;
import ct.common.bean.Producer;
import ct.common.util.DateUtil;
import ct.common.util.NumberUtil;
import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author Jeremy Zheng
 * @Date 2021/2/10 21:25
 * @Version 1.0
 */
//本地数据文件生产者
public class LocalFileProducer implements Producer {

    private DataIn in;
    private DataOut out;
    private volatile boolean flg = true;

    @Override
    public void setIn(DataIn in) {
        this.in = in;
    }

    @Override
    public void setOut(DataOut out) {
        this.out = out;
    }

    @Override
    public void produce() {

        try {
            //读取通讯录数据
            List<Contact> contacts = in.read(Contact.class);
            while (flg) {
                //从通讯录中随机查找2个电话号码（主叫，被叫）
                int call1Index=new Random().nextInt(contacts.size());
                int call2Index;
                while (true){
                    call2Index=new Random().nextInt(contacts.size());
                    if(call1Index!=call2Index){
                        break;
                    }
                }
                Contact call1=contacts.get(call1Index);
                Contact call2=contacts.get(call2Index);

                //生成随机的通话时间（2018年全年的任意时间）
                String startDate="20180101000000";
                String endDate="20190101000000";
                long startTime = DateUtil.parse(startDate,"yyyyMMddHHmmss").getTime();
                long endTime = DateUtil.parse(endDate,"yyyyMMddHHmmss").getTime();
                //通话时间
                long callTime = startTime+(long)((endTime-startTime)*Math.random());
                //通话时间字符串
                String callTimeString = DateUtil.format(new Date(callTime),"yyyyMMddHHmmss");

                //生成随机的通话时长
                String duration= NumberUtil.format(new Random().nextInt(3000),4);

                //生成通话记录
                CallLog log =new CallLog(call1.getTel(),call2.getTel(),callTimeString,duration);

                System.out.println(log);//先看看在控制台上能不能正常输出
                //将通话记录刷写到数据文件中
                out.write(log);

                //每0.5s一条数据
                Thread.sleep(500);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //关闭生产者
    @Override
    public void close() throws IOException {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}
